﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Wicture.DbRESTFul.SQL;
using Wicture.EPC.Common;
using Wicture.EPC.Service.V1.Models;
using Wicture.EPC.Service.V1.Repository;

namespace Wicture.EPC.Service.V1.QueryInterceptors.Interceptors
{
    public class KiaQueryInterceptor : BaseQueryInterceptor
    {
        private string[] brandCodes = new string[] { "kia" };

        public override string[] BrandCodes => brandCodes;
        protected override dynamic GetModelsByPart(DbRESTFulRepository repository, int pageIndex, int pageSize, string brandCode, string partCode)
        {
            return repository.Invoke(CSIs.Query_Models_By_Part_For_Kia, new { pageIndex, pageSize, brandCode, partCode });
        }
        protected override IEnumerable<dynamic> GetHotPoints(DbRESTFulRepository repository, JObject param)
        {
            return repository.Invoke(CSIs.Query_HotPoints_For_Bjhd, param);
        }
        protected override List<Query_PartsResult> BuildPartResults(List<Query_PartsResult> result)
        {
            List<Query_PartsResult> res = new List<Query_PartsResult>();

            //特殊处理,超过1w取3千
            if (result != null && result.Count() > 3000)
            {
                var groupedList = result.GroupBy(e => e.imageSN);
                foreach (var item in groupedList)
                {
                    res.AddRange(item.Where(e => e.belongToVin).Take(20));
                }
            }
            else
            {
                res = result;
            }

            return res.OrderBy(q => q.imageSN).ToList();
        }

        protected override List<Query_PartsResult> GetParts(DbRESTFulRepository repository, JObject param)
        {
            return repository.Invoke<Query_PartsResult>(CSIs.Query_Parts, param);
        }

        public override object QueryPartDetail(DbRESTFulRepository repository, JObject param)
        {
            param.RemoveEmptyValues();
            var brandCode = param.Value<string>(ParamFields._brandCode);
            var partId = param.Value<string>(ParamFields._partId);
            var partCode = param.Value<string>(ParamFields._partCode);
            dynamic data = null;

            if (string.IsNullOrEmpty(partId))
            {
                data = repository.Invoke(CSIs.Get_Part_Detail_Standard, new { partCode, brandCode });
            }
            else
            {
                data = repository.Invoke(CSIs.Get_Usage_Detail_Standard, param);
            }
            if (data == null) return null;
            // 1、基本信息赋值
            Query_Part_DetailResult result = JObject.FromObject(data).ToObject<Query_Part_DetailResult>();

            result.replacements = new List<Query_Part_DetailResult._replacement>();
            //3.替换件处理
            if (!string.IsNullOrEmpty(result.replacementPartNo))
            {
                List<Query_Part_DetailResult._replacement> replacements = repository.Invoke<Query_Part_DetailResult._replacement>(CSIs.Query_Parts_By_Codes, new { partCodes = result.replacementPartNo.Split(',') });
                if (replacements != null)
                {
                    foreach (var replacement in replacements)
                    {
                        replacement.brandCode = brandCode;
                        replacement.brandName = BrandDictionary.GetName(brandCode);
                        result.replacements.Add(replacement);
                    }
                }
            }
            //4.配件附加属性处理
            var props = new List<Query_Part_DetailResult._prop>
            {
                new Query_Part_DetailResult._prop { key = "件数", value = result.quantity ?? string.Empty },
            };
            result.props = props.Where(q => !string.IsNullOrEmpty(q.value)).ToList();
            return result;
        }

        protected override List<Get_Model_InfoResult._props> GetModelProps(DbRESTFulRepository repository, JObject param)
        {
            var ret = new List<Get_Model_InfoResult._props>();
            List<Query_CrumbResult> list = repository.Invoke<Query_CrumbResult>(CSIs.Get_Crumbs_App, param);
            foreach (var info in list)
            {
                if (string.IsNullOrEmpty(info.id))
                {
                    continue;
                }
                if (info.fieldCode == "model")
                {
                    ret.Add(new Get_Model_InfoResult._props { key = "大类", value = info.name.Split('@')[0] });
                    ret.Add(new Get_Model_InfoResult._props { key = "车型", value = info.name.Split('@')[1] });
                }
                else
                {
                    ret.Add(new Get_Model_InfoResult._props { key = info.fieldName?.Replace("选择", ""), value = info.name });
                }
            }
            return ret;
        }
    }
}



